package com.talent.service.iot.mqtt.codec.decoder;

import com.talent.service.iot.mqtt.model.fix.TlMqttFixedHead;
import com.talent.service.iot.mqtt.model.request.TlMqttPubCompReq;
import com.talent.service.iot.mqtt.model.variable.TlMqttPubCompVariableHead;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.mqtt.MqttMessageType;
import org.springframework.stereotype.Component;

/**
 * @ClassName：TlConnectDecoder
 * @Author: hszhou
 * @Date: 2024/11/30 15:12
 * @Description: QOS2的消息接受者发给发送者的报文确定解码器
 */
@Component
public class TlMqttPubCompDecoder {

    public TlMqttPubCompReq build(ByteBuf buf, int remainingLength){
        TlMqttFixedHead fixedHead = decodeFixedHeader(remainingLength);
        TlMqttPubCompVariableHead variableHead = decodeVariableHeader(buf);
        return new TlMqttPubCompReq(fixedHead,variableHead);

    }

    TlMqttFixedHead decodeFixedHeader(int remainingLength) {
        TlMqttFixedHead fixedHead =new TlMqttFixedHead();
        fixedHead.setMessageType(MqttMessageType.PUBCOMP);
        fixedHead.setLength(remainingLength);
        return fixedHead;
    }

    TlMqttPubCompVariableHead decodeVariableHeader(ByteBuf buf) {
        int messageId = buf.readUnsignedShort();
        return TlMqttPubCompVariableHead.of(messageId);
    }



}
